import React, { Component } from 'react';

import 'moment/locale/zh-cn';
import { Row, Col, Form, Input, Select, DatePicker, Button, Space } from 'antd'
import WarningTable from './components/WarningTable';
import * as actions from './store/action';
import { connect } from 'react-redux';
import styles from './index.less'
import QueryWarning from './components/QueryWarning';
import {getVidiconWarning,getWarningByASTTMD2} from '../Center/request/action'
class Index extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableStatus: 'default', //default-默认，query-查询
            queryAdress: {}, //更具地址的查询条件
            pageIndex: 1,//默认当前页
            pageSize: 10,
            information: ''
        }
    }
    componentDidMount() {
        this.props.getAllWarning({ pageIndex: 0, pageSize: 10 });
    }

    render() {
        const { warninglist, totalCount} = this.props;
        const { pageIndex, pageSize } = this.state;
        return (
            <div className={styles.totall}>
                <Row>
                    <Col span={24}>
                        <QueryWarning
                            information={this.props.information}
                            id={this.props.id}
                            getWarningByASTTMD2={(values)=>{
                                this.setState({
                                    tableStatus: 'query',
                                    queryAdress: values,
                                    pageIndex: 1,
                                    pageSize: 10,
                                  
                                })
                                this.props.getWarningByASTTMD2(values)}}
                            handleQueryByAdress={(values) => {
                                this.setState({
                                    tableStatus: 'query',
                                    queryAdress: values,
                                    pageIndex: 1,
                                    pageSize: 10,
                                })
                                this.props.getWarningByASTTMD(values)
                            }}
                            handleResetQuery={() => {
                                this.setState({
                                    tableStatus: 'default',
                                    queryAdress: {},
                                    pageIndex: 1,
                                    pageSize: 10,
                                })
                                this.props.information==undefined?this.props.getAllWarning({ pageIndex: 0, pageSize: 10 }):this.props.getVidiconWarning({       
                                id:this.props.id,
                                pageIndex:0,
                                pageSize:10})
                            }}
                            onSearchAfter={(value) => {
                                this.onSearchAfter(value)
                            }} />
                    </Col>
                    <Col>
                        <WarningTable style={{ marginBottom: '20px' }}
                            pageSize={pageSize}
                            pageIndex={pageIndex}
                            totalCount={totalCount}
                            warninglist={warninglist}
                            information={this.props.information}               //  这里判断是否是查看跟多点击过来
                            handlePageChange={this.handlePageChange} />
                            
                    </Col>
                </Row>
            </div>
        )

    }
    handlePageChange = (pageIndex, pageSize) => {
        this.setState({
            pageIndex,
            pageSize
        })
        if (this.state.tableStatus === "default") {
            if(this.props.information==undefined){this.props.getAllWarning({ pageIndex: pageIndex - 1, pageSize })
        }
        else{
            let information ={
                id:this.props.id,
                pageIndex:pageIndex - 1,
                pageSize:pageSize
            }
            this.props.getVidiconWarning(information)
        }

        } else {
            if(this.props.information==undefined){
            let obj = this.state.queryAdress;
            obj.pageIndex = pageIndex - 1
            obj.pageSize = pageSize
            this.props.getWarningByASTTMD(obj)

            }
            if(this.props.information!=undefined){
                let obj = this.state.queryAdress;
                obj.pageIndex = pageIndex - 1
                obj.pageSize = pageSize
                this.props.getWarningByASTTMD2(obj)
              
            }
        }
    }
    // 根据表格操作状态，加载数据
    handleStatusLoadData = () => {
        let info = {}
        if (this.state.tableStatus === "default") {
            info.pageIndex = this.state.pageIndex - 1;
            info.pageSize = this.state.pageSize;
        } else {
            info = this.state.queryAdress
        }
        return info
    }

    onSearchAfter = (value) => {
        const { warninglist } = this.props;
        let array = [];

        if (value === "" || value === undefined) {
            this.setState({
                warninglist: warninglist
            })
        } else {
            warninglist.map(i => {
                if (Object.values(i).toString().includes(value)) {
                    array.push(i);
                }
                return null;
            })
            this.setState({
                warninglist: array
            })
        }
    }
}
const mapStateToProps = (state) => ({
    warninglist: state.getAllWarning.warninglist,
    totalCount: state.getAllWarning.totalCount
})
const mapDispatchToProps = (dispatch) => ({
    getAllWarning(value) {
        dispatch(actions.getAllWarningRequest(value))
    },
    getWarningByASTTMD(information) {
        dispatch(actions.getWarningByASTTMD(information))
    },
    getVidiconWarning(information){
        dispatch(getVidiconWarning(information))
    },
    getWarningByASTTMD2(information){
        dispatch(getWarningByASTTMD2(information))
    }
    
})
export default connect(mapStateToProps, mapDispatchToProps)(Index);
